//
//  MyLoansInfoVC.m
//  JinFuThree
//
//  Created by 毛韶谦 on 2018/9/6.
//  Copyright © 2018年 毛韶谦. All rights reserved.
//

#import "MyLoansInfoVC.h"
#import "RefundPlanCell.h"
#import "LonsScheduleCell.h"
#import "LonsInfoCell.h"
#import "RefundHintView.h"

@interface MyLoansInfoVC ()<UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate, RefundHintDelegate>

@property (nonatomic, strong) UILabel *infoLabel;
@property (nonatomic, strong) UILabel *refundLabel;
@property (nonatomic, strong) UIImageView *lineImageView;

@property (nonatomic, strong) UIView *leftHeaderView;
@property (nonatomic, strong) UIView *rightHeaderView;

@property (nonatomic, strong) UIScrollView *mainScrollView;
@property (nonatomic, strong) UITableView *leftTabelView;
@property (nonatomic, strong) UITableView *rightTabelView;

@property (nonatomic, strong) UIView *leftBottomView;
@property (nonatomic, strong) UIView *rightBottomView;

@end

@implementation MyLoansInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNavigationBar];
    [self initUI];
    // Do any additional setup after loading the view.
}

- (void)initNavigationBar {
    
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    
    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button1 setImage:[[UIImage imageNamed:@"BackArrow"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    button1.frame = CGRectMake(0, 2.5, 30, 41.5f);
    [button1 addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    
    [backgroundView addSubview:button1];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button2 setImage:[[UIImage imageNamed:@"nav_close"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    button2.frame = CGRectMake(30, 2.5, 30, 41.5f);
    [button2 addTarget:self action:@selector(backRootAction) forControlEvents:UIControlEventTouchUpInside];
    
    [backgroundView addSubview:button2];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backgroundView];
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 173, 44)];
    [headerView addSubview:self.infoLabel];
    [headerView addSubview:self.refundLabel];
    [headerView addSubview:self.lineImageView];
    
    self.navigationItem.titleView = headerView;
    
}
- (void)backAction {
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)backRootAction {
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}


- (void)initUI {
    
    self.leftTabelView.tableHeaderView = self.leftHeaderView;
    self.rightTabelView.tableHeaderView = self.rightHeaderView;
    
    [self updateInfo];
}


- (void)updateInfo {
    
    UIImageView *leftImageView = (UIImageView *)[self.leftHeaderView viewWithTag:2];
    leftImageView.backgroundColor = [UIColor yellowColor];
    UILabel *leftLabel1 = (UILabel *)[self.leftHeaderView viewWithTag:3];
    leftLabel1.text = @"还款中···";
    
    UILabel *rightLabel1 = (UILabel *)[self.rightHeaderView viewWithTag:3];
    rightLabel1.text = @"5000.00";
    self.leftBottomView.hidden = NO;
    self.rightBottomView.hidden = NO;
}


//还款计划 还款事件
- (void)huanKuanAction {
    
    [self showHintView];
}


//展示还 款提示信息页面
-(void)showHintView {
    
    
    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidthMao, ScreenHeight)];
    backgroundView.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.4/1.0];
    backgroundView.tag = 12;
    
    RefundHintView *hintView = [[RefundHintView alloc] initWithFrame:CGRectMake(0, 0, 311, 384)];
    hintView.center = CGPointMake(ScreenWidthMao/2.f, ScreenHeight/2.f);
    hintView.delegate = self;
    [hintView updateInfo];
    [backgroundView addSubview:hintView];
    
    [UIView animateWithDuration:0.3 animations:^{
        [[HFQCommonTools lastWindow] addSubview:backgroundView];
    }];
}

//确定点击事件
- (void)ClickSureBtnAction {
    
    NSLog(@"确定点击事件");
}

#pragma mark tableView代理方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    if ([tableView isEqual:self.leftTabelView]) {
        return 2;
    }
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if ([tableView isEqual:self.leftTabelView]) {
        if (section == 0) {
            
            return 10;
        }else {
            
            return 5;
        }
    }
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([tableView isEqual:self.leftTabelView]) {
        if (indexPath.section == 0) {
            
            return 30;
        }else {
            return 73;
        }
    }
    return 90;
}
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//
//    if ([tableView isEqual:self.leftTabelView]) {
//        if (indexPath.section == 0) {
//
//            return 30;
//        }else {
//            return 60;
//        }
//    }
//    return 90;
//}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if ([tableView isEqual:self.leftTabelView]) {
        if (section == 0) {
            return 14;
        }
    }
    
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    if ([tableView isEqual:self.leftTabelView]) {
        if (section == 0) {
            return 14;
        }
    }
    return 0.01f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([tableView isEqual:self.leftTabelView]) {
        
        if (indexPath.section == 0) {
            
            LonsInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LonsInfoCell"];
            if (!cell) {
                cell = [[LonsInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LonsInfoCell"];
                cell.backgroundColor = [UIColor whiteColor];
            }
            [cell updateInfoCellWithModel];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            return cell;
        }else {
            
            LonsScheduleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LonsScheduleCell"];
            if (!cell) {
                cell = [[LonsScheduleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LonsScheduleCell"];
                cell.backgroundColor = [UIColor whiteColor];
            }
            if (indexPath.row == 0) {
                cell.leftTopImageView.hidden = YES;
            }else if (indexPath.row == 4) {
                cell.leftBottomImageView.hidden = YES;
            }else {
                cell.leftBottomImageView.hidden = NO;
                cell.leftTopImageView.hidden = NO;
            }
            [cell updateInfoCellWithModel:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            return cell;
        }
    }else {
        
        RefundPlanCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RefundPlanCell"];
        if (!cell) {
            cell = [[RefundPlanCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RefundPlanCell"];
            cell.backgroundColor = [UIColor whiteColor];
        }
        
        [cell updateInfoCellWithModel:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    
    return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    if ([tableView isEqual:self.leftTabelView]) {
        if (section == 0) {
            UIImageView *lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 13.5, ScreenWidthMao - 30, 0.5)];
            lineImageView.backgroundColor = ThemeMaoLineColor;
            [view addSubview:lineImageView];
        }
    }
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
}

#pragma mark --------scrollViewDelegate--------------

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if ([scrollView isEqual:self.mainScrollView]) {
        
        if (scrollView.contentOffset.x == 0) {
            [UIView animateWithDuration:0.3 animations:^{
                
                self.infoLabel.font = [UIFont boldSystemFontOfSize:16];
                self.infoLabel.textColor = HexColor(0x383838);
                self.refundLabel.font = [UIFont systemFontOfSize:16];
                self.refundLabel.textColor = HexColor(0x666666);
                self.lineImageView.frame = CGRectMake(0, 42, 68, 2);
            }];
            
        }else if (scrollView.contentOffset.x == ScreenWidthMao){
            [UIView animateWithDuration:0.3 animations:^{
                
                self.infoLabel.font = [UIFont systemFontOfSize:16];
                self.infoLabel.textColor = HexColor(0x666666);
                self.refundLabel.font = [UIFont boldSystemFontOfSize:16];
                self.refundLabel.textColor = HexColor(0x383838);
                self.lineImageView.frame = CGRectMake(105 , 42, 68, 2);
            }];
            
        }else{
            self.lineImageView.frame = CGRectMake(105.f*scrollView.contentOffset.x/ScreenWidthMao , 42, 68, 2);
        }
    }
}

-(void)infoViewShowAction {
    
    [UIView animateWithDuration:0.3 animations:^{
        
        self.infoLabel.font = [UIFont boldSystemFontOfSize:16];
        self.infoLabel.textColor = HexColor(0x383838);
        self.refundLabel.font = [UIFont systemFontOfSize:16];
        self.refundLabel.textColor = HexColor(0x666666);
        self.lineImageView.frame = CGRectMake(0, 42, 68, 2);
        self.mainScrollView.contentOffset = CGPointMake(0, 0);
    }];
}

-(void)refundPlanViewShowAction{
    
    
    [UIView animateWithDuration:0.3 animations:^{
        
        self.infoLabel.font = [UIFont systemFontOfSize:16];
        self.infoLabel.textColor = HexColor(0x666666);
        self.refundLabel.font = [UIFont boldSystemFontOfSize:16];
        self.refundLabel.textColor = HexColor(0x383838);
        self.lineImageView.frame = CGRectMake(105 , 42, 68, 2);
        self.mainScrollView.contentOffset = CGPointMake(ScreenWidthMao, 0);
        
    }];
}




- (UILabel *)infoLabel {
    
    if (!_infoLabel) {
        _infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 7, 68, 23)];
        _infoLabel.font = [UIFont boldSystemFontOfSize:16];
        _infoLabel.textColor = HexColor(0x383838);
        _infoLabel.text = @"分期详情";
        _infoLabel.userInteractionEnabled = YES;
        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(infoViewShowAction)];
        [_infoLabel addGestureRecognizer:gesture];
    }
    return _infoLabel;
}
- (UILabel *)refundLabel {
    
    if (!_refundLabel) {
        _refundLabel = [[UILabel alloc] initWithFrame:CGRectMake(105, 7, 68, 23)];
        _refundLabel.font = [UIFont systemFontOfSize:16];
        _refundLabel.textColor = HexColor(0x666666);
        _refundLabel.text = @"还款计划";
        _refundLabel.userInteractionEnabled = YES;
        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refundPlanViewShowAction)];
        [_refundLabel addGestureRecognizer:gesture];
    }
    return _refundLabel;
}

- (UIImageView *)lineImageView {
    
    if (!_lineImageView) {
        _lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 42, 68, 2)];
        _lineImageView.backgroundColor = ThemeMaoColor;
    }
    return _lineImageView;
}

- (UIScrollView *)mainScrollView {
    
    if (!_mainScrollView) {
        _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidthMao, ScreenHeight - SafeAreaTopHeight)];
        _mainScrollView.contentSize = CGSizeMake(ScreenWidthMao * 2, ScreenHeight - SafeAreaTopHeight - SafeAreaBottomHeight);
        _mainScrollView.showsVerticalScrollIndicator = NO;
        _mainScrollView.showsHorizontalScrollIndicator = NO;
        _mainScrollView.bounces = NO;        
        _mainScrollView.pagingEnabled = YES;
        _mainScrollView.delegate = self;
        
        [self.view addSubview:_mainScrollView];
    }
    return _mainScrollView;
}
- (UITableView *)leftTabelView {
    
    if (!_leftTabelView) {
        _leftTabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidthMao, ScreenHeight - SafeAreaTopHeight - 50) style:UITableViewStyleGrouped];
        _leftTabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _leftTabelView.delegate = self;
        _leftTabelView.dataSource = self;
        _leftTabelView.backgroundColor = ListMaoBackgroundColor;
        
        [self.mainScrollView addSubview:_leftTabelView];
    }
    return _leftTabelView;
}
- (UITableView *)rightTabelView {
    
    if (!_rightTabelView) {
        _rightTabelView = [[UITableView alloc] initWithFrame:CGRectMake(ScreenWidthMao, 0, ScreenWidthMao, ScreenHeight - SafeAreaTopHeight - 50) style:UITableViewStyleGrouped];
        _rightTabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _rightTabelView.delegate = self;
        _rightTabelView.dataSource = self;
        _rightTabelView.backgroundColor = ListMaoBackgroundColor;
        
        [self.mainScrollView addSubview:_rightTabelView];
    }
    return _rightTabelView;
}

- (UIView *)leftHeaderView {
    
    if (!_leftHeaderView) {
        _leftHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidthMao, 127 + 8)];
        _leftHeaderView.backgroundColor = [UIColor whiteColor];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidthMao/2 - 15, 40, 30, 30)];
        imageView.tag = 2;
        [imageView setContentMode:UIViewContentModeScaleAspectFit];
        [_leftHeaderView addSubview:imageView];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(imageView.frame) + 20, ScreenWidthMao, 16)];
        label.tag = 3;
        label.font = [UIFont systemFontOfSize:15];
        label.textColor = HexColor(0x666666);
        [label setTextAlignment:NSTextAlignmentCenter];
        [_leftHeaderView addSubview:label];
        
        UIImageView *lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 127, ScreenWidthMao, 8)];
        lineImageView.backgroundColor = ListMaoBackgroundColor;
        [_leftHeaderView addSubview:lineImageView];
    }
    return _leftHeaderView;
}
- (UIView *)rightHeaderView {
    
    if (!_rightHeaderView) {
        _rightHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidthMao, 107 + 8)];
        _rightHeaderView.backgroundColor = [UIColor whiteColor];
        
        UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 22, ScreenWidthMao, 42)];
        label1.tag = 3;
        label1.font = [UIFont boldSystemFontOfSize:36];
        label1.textColor = HexColor(0x333333);
        [label1 setTextAlignment:NSTextAlignmentCenter];
        [_rightHeaderView addSubview:label1];
        UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 67, ScreenWidthMao, 21)];
        label2.font = [UIFont systemFontOfSize:15];
        label2.text = @"分期金额(元)";
        label2.textColor = HexColor(0x555555);
        [label2 setTextAlignment:NSTextAlignmentCenter];
        [_rightHeaderView addSubview:label2];
        
        UIImageView *lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 107, ScreenWidthMao, 8)];
        lineImageView.backgroundColor = ListMaoBackgroundColor;
        [_rightHeaderView addSubview:lineImageView];
    }
    return _rightHeaderView;
}

- (UIView *)leftBottomView {
    
    if (!_leftBottomView) {
        _leftBottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight - SafeAreaTopHeight - 50, ScreenWidthMao, 50)];
        _leftBottomView.backgroundColor = [UIColor whiteColor];
        UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidthMao, 50)];
        label2.font = [UIFont systemFontOfSize:15];
        label2.text = @"立即退货";
        label2.textColor = ThemeMaoColor;
        [label2 setTextAlignment:NSTextAlignmentCenter];
        [_leftBottomView addSubview:label2];
        
        UIImageView *lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidthMao, 0.5)];
        lineImageView.backgroundColor = ThemeMaoLineColor;
        [_leftBottomView addSubview:lineImageView];
        
        [self.mainScrollView addSubview:_leftBottomView];
    }
    return _leftBottomView;
}
- (UIView *)rightBottomView {
    
    if (!_rightBottomView) {
        _rightBottomView = [[UIView alloc] initWithFrame:CGRectMake(ScreenWidthMao, ScreenHeight - SafeAreaTopHeight - 50, ScreenWidthMao, 50)];
        _rightBottomView.backgroundColor = [UIColor whiteColor];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.backgroundColor = ThemeMaoColor;
        [btn setTitle:@"立即还款" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        btn.frame = CGRectMake(ScreenWidthMao*312.f/750.f, 0, ScreenWidthMao*440.f/750.f, 50);
        [btn addTarget:self action:@selector(huanKuanAction) forControlEvents:UIControlEventTouchUpInside];
        [_rightBottomView addSubview:btn];
        UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidthMao*312.f/750.f, 50)];
        label2.font = [UIFont systemFontOfSize:15];
        label2.text = @"已选833,33元";
        label2.textColor = HexColor(0x333333);
        [label2 setTextAlignment:NSTextAlignmentCenter];
        label2.tag = 1;
        [_rightBottomView addSubview:label2];
        
        UIImageView *lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidthMao, 0.5)];
        lineImageView.backgroundColor = ThemeMaoLineColor;
        [_rightBottomView addSubview:lineImageView];
        
        [self.mainScrollView addSubview:_rightBottomView];
    }
    return _rightBottomView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
